package com.sx.zongzhi.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sx.zongzhi.R;
import com.sx.zongzhi.bean.BuildingBean;
import com.sx.zongzhi.widget.PanelAdapter;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by XZY on 2017/01/15.
 *
 */
public class BuildingAdapter extends PanelAdapter {

    private Context context;
    private List<BuildingBean.BuildLayoutBean> buildLayoutBeen;
    private OnItemClickListener mOnItemClickListener;
    private int row,column;
    private DecimalFormat df=new DecimalFormat("00");

    public BuildingAdapter(Context context, List<BuildingBean.BuildLayoutBean> buildLayoutBeen
            ,int row,int column) {
        this.context=context;
        this.buildLayoutBeen=buildLayoutBeen;
        this.row=row;
        this.column=column;
    }

    @Override
    public int getRowCount() {
        return row;
    }

    @Override
    public int getColumnCount() {
        return column;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int row, final int column) {
        TitleViewHolder viewHolder = (TitleViewHolder) holder;
        BuildingBean.BuildLayoutBean bean=null;//buildLayoutBeen.get(this.column*(row-1)+column-1);
        for (int i=0;i<buildLayoutBeen.size();i++){
            //保证行列对应上
            if (buildLayoutBeen.get(i).getLayoutRow()==row&&buildLayoutBeen.get(i).getLayoutColumn()==column){
                bean=buildLayoutBeen.get(i);
                break;
            }
        }
        if (bean!=null){
            viewHolder.titleTextView.setText(df.format(bean.getLayoutRow())+""+df.format(bean.getLayoutColumn()));
            if (bean.isIsHouse()){
                viewHolder.imageView.setImageResource(R.mipmap.img_building_binding);
                viewHolder.titleTextView.setTextColor(Color.parseColor("#1DAAF4"));
            }else {
                viewHolder.imageView.setImageResource(R.mipmap.img_building_unbounded);
                viewHolder.titleTextView.setTextColor(Color.parseColor("#999999"));
            }
        }

        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(v,row,column);
                }
            });
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return  new TitleViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_building, parent, false));
    }

    private static class TitleViewHolder extends RecyclerView.ViewHolder {
        public TextView titleTextView;
        public ImageView imageView;

        public TitleViewHolder(View view) {
            super(view);
            this.titleTextView = (TextView) view.findViewById(R.id.tv_Num);
            this.imageView= (ImageView) view.findViewById(R.id.imageView);
        }
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    /**
     * 处理item的点击事件和长按事件
     */
    public interface OnItemClickListener {
        void onItemClick(View view, int row,int column);
    }
}
